﻿@model IEnumerable<LotteryVoteMVC.Models.SettleResult>
@using LotteryVoteMVC.Core
@using LotteryVoteMVC.Models
@using LotteryVoteMVC.Resources
@using LotteryVoteMVC.Utility
@{
    ViewBag.Title = Resource.WinLost;
    Layout = "~/Views/Shared/Main.cshtml";
}
@section Header{
    <script src="@Url.Content("~/Scripts/Customer/SearchHelper.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.datepicker.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.datepicker-" + CultureHelper.GetCurrentCulture() + ".js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/Customer/TableBase.js")" type="text/javascript"></script>
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.theme.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.datepicker.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/TableBase.css")" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        $(document).ready(function () {
            $("#FromDate").attr("readonly", "readonly").datepicker();
            $("#ToDate").attr("readonly", "readonly").datepicker();
            $("#today").click(function () {
                var date = new Date();
                var dateFormat = parseDateFormat(date);
                $("#FromDate").val(dateFormat);
                $("#ToDate").val(dateFormat);
            });
            $("#yesterday").click(function () {
                var date = new Date();
                var yesterday = new Date();
                yesterday.setDate(date.getDate() - 1);
                var dateFormat = parseDateFormat(yesterday);
                $("#FromDate").val(dateFormat);
                $("#ToDate").val(dateFormat);
            });
            $("#thisweek").click(function () {
                var date = new Date();
                var today = date.getDay();
                var diff = 0;
                if (today > 1)
                    diff = today - 1;
                else if (today == 0)
                    diff = 6;
                var fromDate = new Date();
                fromDate.setDate(date.getDate() + diff * -1);
                var toDate = new Date();
                toDate.setTime(fromDate.getTime() + 6 * 24 * 60 * 60 * 1000);
                $("#FromDate").val(parseDateFormat(fromDate));
                $("#ToDate").val(parseDateFormat(toDate));
            });
            $("#lastweek").click(function () {
                var date = new Date();
                var today = date.getDay();
                var diff = 0;
                if (today > 1)
                    diff = today - 1;
                else if (today == 0)
                    diff = 6;
                var fromDate = new Date();
                fromDate.setDate(date.getDate() + diff * -1 - 7);
                var toDate = new Date();
                toDate.setTime(fromDate.getTime() + 6 * 24 * 60 * 60 * 1000);
                $("#FromDate").val(parseDateFormat(fromDate));
                $("#ToDate").val(parseDateFormat(toDate));
            });
            $("#thismon").click(function () {
                var date = new Date();
                var fromDate = new Date();
                fromDate.setDate(1);
                $("#FromDate").val(parseDateFormat(fromDate));
                $("#ToDate").val(parseDateFormat(date));
            });
            $("#lastmon").click(function () {
                var date = new Date();
                var fromDate = new Date();
                fromDate.setDate(1);
                fromDate.setMonth(date.getMonth() - 1);
                var days = new Date(date.getFullYear(), date.getMonth(), 0).getDate() - 1;
                var toDate = new Date();
                toDate.setTime(fromDate.getTime() + days * 24 * 60 * 60 * 1000);
                $("#FromDate").val(parseDateFormat(fromDate));
                $("#ToDate").val(parseDateFormat(toDate));
            });
            $("input[type='button']").click(function () {
                $("form").submit();
            });
        });
    </script>
    <style type="text/css">
        div.main
        {
            overflow: scroll;
            overflow-y: visible;
            overflow-x: visible;
        }
    </style>
}
@{
    User TargetUser = ViewBag.User;
    int index = 1;
    decimal turnover = 0, totalComm = 0, wl = 0, comm = 0, totalwl = 0, nwl = 0, ncomm = 0, ntotalwl = 0, comWl = 0;
    int betcount = 0;
}
<div id="searchBar">
    @using (Html.BeginForm())
    {
        if (LoginCenter.CurrentUser.Role == Role.Company)
        {
        <span>@Resource.From</span>
        @Html.TextBox("FromDate", DateTime.Today.ToShortDateString())
        <span>@Resource.To</span>
        @Html.TextBox("ToDate", DateTime.Today.ToShortDateString())
        <input type="submit" name="search" id="search" value="@Resource.Search" />
        }
        else { 
            @Html.Hidden("FromDate", DateTime.Today.ToShortDateString())
            @Html.Hidden("ToDate", DateTime.Today.ToShortDateString())
        }
        <input type="button" name="today" id="today" value="@Resource.Today" />
        <input type="button" name="yesterday" id="yesterday" value="@Resource.Yesterday" />
        <input type="button" name="thisweek" id="thisweek" value="@Resource.ThisWeek" />
        <input type="button" name="lastweek" id="lastweek" value="@Resource.LastWeek" />
        if (LoginCenter.CurrentUser.Role == Role.Company)
        {
        <input type="button" name="thismon" id="thismon" value="@Resource.ThisMonth" />
        <input type="button" name="lastmon" id="lastmon" value="@Resource.LastMonth" />
        }
        if (TargetUser.Role > Role.Company)
        {
        @Html.ActionLink(Resource.Return, "Index", new { Id = TargetUser.ParentId, FromDate = ViewBag.From.ToShortDateString(), ToDate = ViewBag.To.ToShortDateString() }, new { @class = "button" })
        }
    }
</div>
<table cellpadding="0" cellspacing="0">
    <thead>
        <tr>
            <td rowspan="2" class="rows">
                @Resource.No
            </td>
            <td rowspan="2" class="rows">
                @Resource.UserName
            </td>
            <td rowspan="2" class="rows">
                @Resource.Name
            </td>
            <td rowspan="2" class="rows">
                @Resource.Currency
            </td>
            <td rowspan="2" class="rows">
                @Resource.BetCount
            </td>
            <td rowspan="2" class="rows">
                @Resource.Turnover
            </td>
            <td rowspan="2" class="rows">
                @Resource.TotalCommission
            </td>
            <td colspan="@(TargetUser.Role == Role.Agent ? 3 : 4)">
                @Resource.ResourceManager.GetString((TargetUser.Role + 1).ToString())
            </td>
            <td colspan="4">
                @Resource.ResourceManager.GetString(TargetUser.Role.ToString())
            </td>
            @if (TargetUser.Role != Role.Company)
            { 
                <td rowspan="2" class="rows">
                    @Resource.Company
                </td>
            }
        </tr>
        <tr>
            @if (TargetUser.Role != Role.Agent)
            {
                <td>
                    %
                </td>
            }
            <td>
                @Resource.WinLost
            </td>
            <td>
                @Resource.Comm
            </td>
            <td>
                @Resource.ToalWinLost
            </td>
            <td>
                %
            </td>
            <td>
                @Resource.WinLost
            </td>
            <td>
                @Resource.Comm
            </td>
            <td>
                @Resource.ToalWinLost
            </td>
        </tr>
    </thead>
    <tbody>
        @foreach (var result in Model)
        { 
            <tr>
                <td>
                    @(index++)
                </td>
                <td>
                    @if (result.User.Role < Role.Guest)
                    {
                        @Html.ActionLink(result.User.UserName, "Index", new { Id = result.UserId, FromDate = ViewBag.From.ToShortDateString(), ToDate = ViewBag.To.ToShortDateString() })
                    }
                    else
                    { 
                        @result.User.UserName
                    }
                </td>
                <td>
                    @result.Name
                </td>
                <td>
                    VND
                </td>
                <td>
                    @result.OrderCount
                </td>
                <td>
                    @Html.ActionLink(result.BetTurnover.ToString("N"), "WinLost", new { Id = result.UserId, FromDate = ViewBag.From.ToShortDateString(), ToDate = ViewBag.To.ToShortDateString() }, new { target = "_blank" })
                </td>
                <td>
                    @result.TotalCommission.ToString("N")
                </td>
                @if (TargetUser.Role != Role.Agent)
                {
                    <td>
                        @result.ShareRate.ToString("P0")
                    </td>
                }
                <td @(result.WinLost < 0 ? "class=red" : string.Empty)>
                    @result.WinLost.ToString("N")
                </td>
                <td @(result.Commission < 0 ? "class=red" : string.Empty)>
                    @result.Commission.ToString("N")
                </td>
                <td @(result.TotalWinLost < 0 ? "class=red" : string.Empty)>
                    @result.TotalWinLost.ToString("N")
                </td>
                <td>
                    @result.UpperShareRate.ToString("p0")
                </td>
                <td @(result.UpperWinLost < 0 ? "class=red" : string.Empty)>
                    @result.UpperWinLost.ToString("N")
                </td>
                <td @(result.UpperCommission < 0 ? "class=red" : string.Empty)>
                    @result.UpperCommission.ToString("N")
                </td>
                <td @(result.UpperTotalWinLost < 0 ? "class=red" : string.Empty)>
                    @result.UpperTotalWinLost.ToString("N")
                </td>
                @if (TargetUser.Role != Role.Company)
                { 
                    <td @(result.CompanyWinLost < 0 ? "class=red" : string.Empty)>@result.CompanyWinLost.ToString("N")
                    </td>
                }
            </tr>
                betcount += result.OrderCount;
                turnover += result.BetTurnover;
                totalComm += result.TotalCommission;
                wl += result.WinLost;
                comm += result.Commission;
                totalwl += result.TotalWinLost;
                nwl += result.UpperWinLost;
                ncomm += result.UpperCommission;
                ntotalwl += result.UpperTotalWinLost;
                comWl += result.CompanyWinLost;
        }
    </tbody>
    <tfoot>
        <tr>
            <td>
            </td>
            <td>
            </td>
            <td>
            </td>
            <td>
                @Resource.Total
            </td>
            <td>@betcount
            </td>
            <td>@turnover.ToString("N")
            </td>
            <td>@totalComm.ToString("N")
            </td>
            @if (TargetUser.Role != Role.Agent)
            {
                <td>
                </td>
            }
            <td @(wl < 0 ? "class=red" : string.Empty)>@wl.ToString("N")
            </td>
            <td @(comm < 0 ? "class=red" : string.Empty)>@comm.ToString("N")
            </td>
            <td @(totalComm < 0 ? "class=red" : string.Empty)>@totalwl.ToString("N")
            </td>
            <td>
            </td>
            <td @(nwl < 0 ? "class=red" : string.Empty)>@nwl.ToString("N")
            </td>
            <td @(ncomm < 0 ? "class=red" : string.Empty)>@ncomm.ToString("N")
            </td>
            <td @(ntotalwl < 0 ? "class=red" : string.Empty)>@ntotalwl.ToString("N")
            </td>
            @if (TargetUser.Role != Role.Company)
            { 
                <td @(comWl < 0 ? "class=red" : string.Empty)>@comWl.ToString("N")
                </td>
            }
        </tr>
    </tfoot>
</table>
